<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="房间名" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入房间名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi-device:room:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi-device:room:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi-device:room:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi-device:room:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="roomList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单位名称" align="center" prop="deptName" />
      <el-table-column label="建筑名称" align="center" prop="buildName" />
      <el-table-column label="房间号" align="center" prop="roomNumber" />
      <el-table-column label="楼层名称" align="center" prop="floorName" />
      <el-table-column label="房间类型" align="center" prop="typeName" />
      <el-table-column label="状态" align="center" prop="stateResult">
        <template #default="scope">
          <div :style="scope.row.stateStyle">{{ scope.row.stateResult }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-device:room:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-device:room:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房间对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="roomRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属楼层" prop="fId">
          <el-select v-model="form.fId" placeholder="请选择" filterable>
            <el-option
              v-for="item in optionsFloor"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间号" prop="roomNumber">
          <el-input v-model="form.roomNumber" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="房间位置" prop="roomPos">
          <el-input v-model="form.roomPos" placeholder="请输入房间位置" />
        </el-form-item>
        <el-form-item label="房间类型" prop="typeId">
          <el-tree-select
            v-model="form.typeId"
            :data="optionsRoomType"
            :props="{
              value: 'id',
              label: 'name',
              children: 'children',
            }"
            placeholder="请选择房间类型"
            check-strictly
          />
        </el-form-item>

        <!-- <el-form-item label="房间号" prop="number">
          <el-input v-model="form.number" placeholder="请输入房间号" />
        </el-form-item> -->
        <el-form-item label="负责人" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择" filterable>
            <el-option
              v-for="item in optionsUser"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="房间面积" prop="syarea">
          <el-input v-model="form.syarea" placeholder="请输入房间面积" />
        </el-form-item>
        <el-form-item label="房间名" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入房间名" />
        </el-form-item>
        <el-form-item label="房间长" prop="roomLong">
          <el-input v-model="form.roomLong" placeholder="请输入房间长" />
        </el-form-item>
        <el-form-item label="房间宽" prop="roomWidth">
          <el-input v-model="form.roomWidth" placeholder="请输入房间宽" />
        </el-form-item>
        <el-form-item label="房间高" prop="roomHeight">
          <el-input v-model="form.roomHeight" placeholder="请输入房间高" />
        </el-form-item>
        <el-form-item label="房间容量" prop="cap">
          <el-input v-model="form.cap" placeholder="请输入房间容量" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择" filterable>
            <el-option
              v-for="item in optionsRoom"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="视频地址" prop="videoUrl">
          <el-input
            v-model="form.videoUrl"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item> -->
        <!-- <el-form-item label="图片地址" prop="pictureUrl">
          <el-input
            v-model="form.pictureUrl"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Room">
const optionsRoom = ref([
  {
    label: "正常",
    value: 0,
  },
  {
    label: "停用",
    value: 1,
  },
]);

import { listUser } from "@/api/system/user";
const optionsUser = ref([]);
function getOptionsUser(tempList) {
  optionsUser.value = [];
  tempList.forEach((element) => {
    optionsUser.value.push({
      label: element.userName,
      value: element.userId * 1,
    });
  });
}
const arrayUser = ref([]);
function getArrayUser() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 20000,
  };
  listUser(queryParams1).then((response) => {
    arrayUser.value = response.rows;
    getOptionsUser(response.rows);
  });
}

import { listRoomType } from "@/api/baseInfo/roomType";
const optionsRoomType = ref([]);
const arrayRoomType = ref([]);
function getOptionsRoomType() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 10000,
  };
  listRoomType(queryParams1).then((response) => {
    optionsRoomType.value = [];
    arrayRoomType.value = response.rows;
    arrayRoomType.value.forEach((item1) => {
      item1.id = item1.id * 1;
    });
    optionsRoomType.value = proxy.handleTree(arrayRoomType.value, "id", "pid");
  });
}

import { listFloor } from "@/api/baseInfo/floor";
const arrayFloor = ref([]);
function getArrayFloor() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 20000,
  };
  listFloor(queryParams1).then((response) => {
    arrayFloor.value = response.rows;
    getOptionsFloor(response.rows);
  });
}
const optionsFloor = ref([]);
function getOptionsFloor(tempList) {
  optionsFloor.value = [];
  tempList.forEach((element) => {
    optionsFloor.value.push({
      label: element.name,
      value: element.id,
    });
  });
}

import {
  listRoom,
  getRoom,
  delRoom,
  addRoom,
  updateRoom,
} from "@/api/baseInfo/room";

const { proxy } = getCurrentInstance();

const roomList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomName: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询房间列表 */
function getList() {
  loading.value = true;
  listRoom(queryParams.value).then((response) => {
    roomList.value = response.rows;
    total.value = response.total;
    getArrayFloor();
    getOptionsRoomType();
    getArrayUser();
    loading.value = false;

    roomList.value.forEach((item1) => {
      if (item1.state == 0) {
        item1.stateResult = "正常";
        item1.stateStyle = {
          color: "green",
        };
      } else if (item1.state == 1) {
        item1.stateResult = "停用";
        item1.stateStyle = {
          color: "red",
        };
      }
    });
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    roomPos: null,
    fId: null,
    number: null,
    typeId: null,
    userId: null,
    description: null,
    syarea: null,
    roomNumber: null,
    roomName: null,
    roomLong: null,
    roomWidth: null,
    roomHeight: null,
    cap: null,
    state: null,
    videoUrl: null,
    pictureUrl: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    remark: null,
    delFlag: null,
  };
  proxy.resetForm("roomRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加房间";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getRoom(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改房间";
  });
}

/** 提交按钮 */
function submitForm() {
  console.log(form.value);
  proxy.$refs["roomRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateRoom(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRoom(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除房间编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delRoom(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi-device/room/export",
    {
      ...queryParams.value,
    },
    `room_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
